package com.woniuxy.demo.epai.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.demo.epai.controller.form.UserRegisterForm;
import com.woniuxy.demo.epai.controller.form.userForm;
import com.woniuxy.demo.epai.dao.MembersMapper;
import com.woniuxy.demo.epai.dao.WalletMapper;
import com.woniuxy.demo.epai.model.Members;
import com.woniuxy.demo.epai.model.User;
import com.woniuxy.demo.epai.dao.UserMapper;
import com.woniuxy.demo.epai.model.Wallet;
import com.woniuxy.demo.epai.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author epai
 * @since 2021-04-12
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WalletMapper walletMapper;


    @Override
    public int getVipStatusById(Integer id) {
        User user = userMapper.selectById(id);
        if(user==null){
            return 3;
        }
        return user.getIsVIP();
    }

    @Override
    public void addVid(userForm user) {

        //更新用户信息到数据库
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",user.getId());
        updateWrapper.set("id_nunber",user.getIdNunber());
        updateWrapper.set("isVIP",1);
        log.info("开始更新会员{}",user.getId());
        userMapper.update(null,updateWrapper);
        //更新钱包
        UpdateWrapper updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("uid",user.getId());
        updateWrapper2.set("guartantee",user.getGuartantee());
        walletMapper.update(null,updateWrapper2);
    }

    @Override
    public User findVip(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    @Cacheable(value = "user_cache",key = "'user_'+#username")
    public Integer userLogin(String username, String password) {
       return userMapper.userLogin(username, password);
    }

    @Override
    public void userRegister(UserRegisterForm userRegisterForm) {
        User user = new User();
        user.setUsername(userRegisterForm.getUsername());
        user.setPassword(userRegisterForm.getPassword());
        user.setEmail(userRegisterForm.getEmail());
        user.setNikeName(userRegisterForm.getNikeName());
        user.setAddress(userRegisterForm.getAddress());
        userMapper.insert(user);
    }


}

